package com.wxl.common.wiget

import android.content.Context
import android.text.TextUtils
import android.view.KeyEvent
import android.view.View
import androidx.core.content.ContextCompat
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.CenterPopupView
import com.wxl.common.R
import com.wxl.common.base.AbsActivity
import io.reactivex.functions.Action
import kotlinx.android.synthetic.main.pop_import_tips.view.*

class LeiGuanTipPop : CenterPopupView {

    private lateinit var lgNum:String
    private lateinit var lgRepeat:String
    private lateinit var lgTotal:String
    private  var  action0: Action?=null
    private lateinit var action1: Action

    constructor(context: Context) : super(context)

    override fun getImplLayoutId(): Int {
        return R.layout.pop_import_tips
    }

    override fun onCreate() {
        super.onCreate()
        leiguanImportNumText.text = lgNum
        leiguanRepeatNumText.text = lgRepeat
        leiguanTotalNumText.text = lgTotal
        if(!TextUtils.isEmpty(lgRepeat) && !TextUtils.equals("0",lgRepeat)){
            leiguanRepeatNumText.setTextColor(ContextCompat.getColor(context,R.color.dot_hong))
        }
        if(action0 == null){
            leiguanTipsCancel.visibility = View.GONE
            leiguanTipsCancelSpace.visibility = View.GONE
        } else {
            leiguanTipsCancel.visibility = View.VISIBLE
            leiguanTipsCancelSpace.visibility = View.VISIBLE
        }
        leiguanTipsCancel.setOnClickListener {
            if(action0 != null) {
                dismiss()
                action0?.run()
            }
        }
        leiguanTipsSure.setOnClickListener {
            dismiss()
            action1.run()
        }
    }

    /**
     * 显示提示框
     */
    fun showTip(lgNum: String,lgRepeat: String,lgTotal: String, action0: Action?, action1: Action): LeiGuanTipPop {
        this.lgNum = lgNum
        this.lgRepeat = lgRepeat
        this.lgTotal = lgTotal
        this.action0 = action0
        this.action1 = action1
        return this
    }


    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_F1 || keyCode == KeyEvent.KEYCODE_BACK) {
            leiguanTipsCancel?.performClick()
        } else if (keyCode == KeyEvent.KEYCODE_F4 ||  keyCode == KeyEvent.KEYCODE_ENTER) {
            leiguanTipsSure?.performClick()
        }
        return true
    }


    companion object {
        fun show(context: Context, lgNum: String,lgRepeat: String,lgTotal: String, action0: Action) {
            val mTipPop = LeiGuanTipPop(context).showTip(lgNum,lgRepeat, lgTotal,null, action0)
            XPopup.Builder(context)
                .dismissOnTouchOutside(false)
                .dismissOnTouchOutside(false)
                .asCustom(mTipPop)
                .show()
            (context as AbsActivity).addPop(mTipPop)
        }
    }
}